;layui.define(function (e) {
    layui.use(['index', 'jquery', 'http', 'common', 'layer', 'laytpl', 'treegrid'], function () {

        var $ = layui.jquery
            , http = layui.http
            , layer = parent.layer === undefined ? layui.layer : top.layer
            , laytpl = layui.laytpl
            , common = layui.common
            , treegrid = layui.treegrid;

        treegrid.config.render = function (viewid, data) {
            var view = document.getElementById(viewid).innerHTML;
            return laytpl(view).render(data) || '';
        };

        var urls = {
            queryPageData: "/core/menu/queryPageData",
            modifyDataDialog: "/core/menu/modifyDataDialog",
            modifySubDataDialog: "/core/menu/modifySubDataDialog",
            modifyMenuPermissionDialog: "/core/menu/permission/modifyMenuPermissionDialog",
            deleteData: "/core/menu/deleteData"
        };
        queryPageData();
        var tableIns, url, index;

        /**
         * 查询页面数据
         */
        function queryPageData () {
            tableIns = treegrid.createNew({
                elem: 'demo',
                view: 'view',
                url: urls.queryPageData,
                id: 'id',
                parentid: 'pid',
                order: 'seq',
                onCollapse: true,
                searchData: {menuName: $('#menuName').val(), pid: 0},
                singleSelect: true,
                loadRow: function (data) {
                    http.ajaxGet(
                        urls.queryPageData,
                        {pid: data.pid},
                        false,
                        function (res) {
                            data.children(res);
                        });
                }
            });
            tableIns.build();
        }

        /**
         * 添加按钮事件
         */
        $("#btnAddPage").click(function () {
            url = urls.modifyDataDialog;
            openDialog('添加菜单', url);
        });
        /**
         * 查询按钮事件
         */
        $("#btnQuery").on("click", function () {
            queryPageData();
        });
        /**
         * 编辑按钮事件
         */
        fn.edit = function () {
            var row = tableIns.getRow();
            url = urls.modifyDataDialog + "?id=" + row.id;
            openDialog('修改菜单', url);
        };
        /**
         * 删除按钮事件
         */
        fn.del = function () {
            var row = tableIns.getRow();
            layer.confirm('删除该菜单将会删除菜单相关的数据,确定要删除吗？', {icon: 3, title: '提示信息'}, function (index) {
                http.post({
                    url: urls.deleteData + "/" + row.id, data: {}, success: function () {
                        common.showSuccessTips('数据删除成功', function () {
                            fn.refresh(true);
                        });
                    }
                });
            });
        };
        /**
         * 添加子菜单
         */
        fn.addSub = function () {
            var row = tableIns.getRow();
            url = urls.modifySubDataDialog + "?id=" + row.id;
            openDialog('添加子菜单', url);
        };
        fn.addPermission = function () {
            var row = tableIns.getRow();
            url = urls.modifyMenuPermissionDialog + "?menuId=" + row.id;
            openDialog('添加菜单权限', url);

        };
        /**
         * 刷新页面数据
         */
        fn.refresh = function (reload) {
            if (index)
                layui.layer.close(index);
            if (reload)
                queryPageData();
        };
        /**
         * 打开编辑对话框
         * @param title 对话框标题
         * @param url 对话框内容url
         */
        var openDialog = function (title, url) {
            index = layui.layer.open({
                type: 2,
                title: title,
                shade : [0.8, '#C8E6C9'],
                maxmin: true,
                area: ['600px', '550px'],
                content: url,
                btn: ['保存', '关闭'],
                btnAlign: 'c',
                //skin: 'layui-layer-admin',
                yes: function (index, layero) {
                    layero.find('iframe').contents().find("#formAdd").find("#btnAdd").click();
                },
                btn2: function (index) {
                    layui.layer.close(index);
                }
            });
        };
    });

    e("core/menu", {})
});